package com.ruoyi.game.controller;

import cn.hutool.core.date.DateUtil;
import cn.shuibo.annotation.Decrypt;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.game.domain.GameEcpmRecord;
import com.ruoyi.game.domain.GamePacketRecord;
import com.ruoyi.game.domain.GameUser;
import com.ruoyi.game.domain.GameUserConfig;
import com.ruoyi.game.service.*;
import com.ruoyi.game.utils.ValidateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.Objects;

/**
 * 记录接口
 */
@RestController
@RequestMapping("/api/record")
public class CustomGameRecordController {

    @Autowired
    private IGameEcpmRecordService gameEcpmRecordService;
    @Autowired
    private IGameUserService gameUserService;
    @Autowired
    private ValidateTime validateTime;
    @Autowired
    private IGamePacketRecordService gamePacketRecordService;
    @Autowired
    private IGameUserConfigService iGameUserConfigService;
    /**
     * 增加ECPM记录
     * @return 返回
     */
    @Decrypt
    @RepeatSubmit
    @PostMapping("/addEcpmRecord")
    public AjaxResult addEcpmRecord(@RequestBody JSONObject json){
        String userId = json.getString("userId");
        //ECPM数值
        String ecpmTotal = json.getString("ecpmTotal");
        //广告商
        String ecpmAd = json.getString("ecpmAd");
        //观看什么类型
        String ecpmDesc = json.getString("ecpmDesc");
        //用户IP地址
        String userIp = json.getString("userIp");
        //用户设备
        String userDeviceId = json.getString("userDeviceId");
        //当前时间戳
        String timestamp = json.getString("timestamp");
        //时间戳验证
        if (!validateTime.validateTime(timestamp)) {
            return AjaxResult.error(102,"异常请求");
        }
        //获取到用户
        GameUser gameUser = gameUserService.selectGameUserByUserId(Long.valueOf(userId));

        if(gameUser == null){
            return AjaxResult.error(101,"用户不存在");
        }
        GameEcpmRecord gameEcpmRecord = null;
        //首先判断用户是否还能看广告
        GameUserConfig gameUserConfig = iGameUserConfigService.selectGameUserConfigByUserId(gameUser.getUserId());
        //总看次数
        Long adTotal = gameUserConfig.getAdTotal();
        //已看次数
        Long adTotalReady = gameUserConfig.getAdTotalReady();

        if(adTotalReady>=adTotal){
            //已经没有奖励了 开始拒绝
            return AjaxResult.error(101,"今日奖励发放已达上限");
        }else{
            //观看次数+1
            gameUserConfig.setAdTotalReady(adTotalReady + 1);
        }

        //判断剩余次数
        long adTotalSurplus = adTotal - adTotalReady;

        if(adTotalSurplus == 0){
            //设置为假
            gameUserConfig.setAdIs(1);
        }



        //插入数据库
        iGameUserConfigService.updateGameUserConfig(gameUserConfig);

        try {
            //创建类
            gameEcpmRecord = new GameEcpmRecord();
            gameEcpmRecord.setEcpmDesc(ecpmDesc);
            gameEcpmRecord.setEcpmTotal(ecpmTotal);
            gameEcpmRecord.setUserIp(userIp);
            gameEcpmRecord.setUserDeviceId(userDeviceId);
            gameEcpmRecord.setEcpmAd(ecpmAd);
            gameEcpmRecord.setUserName(gameUser.getUserName());
            gameEcpmRecord.setUserId(gameUser.getUserId());
            gameEcpmRecord.setCreationTime(DateUtil.date());
            //计算返回抽成比例
            BigDecimal userProportion = gameUser.getUserProportion();
            //计算比例
            BigDecimal proportion = userProportion.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
            //计算用户收入
            BigDecimal ecpm = new BigDecimal(ecpmTotal);
            //   30046.168 / 100000  = 15.234  //用户所得
            //实际收入
            BigDecimal userTotal;
            userTotal = ecpm.divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP).multiply(proportion);
            //用户收入 计算成功并且更新数据库和更新记录
            System.out.println(userTotal);
            gameEcpmRecord.setEcpmMoney(userTotal);
            gameEcpmRecordService.insertGameEcpmRecord(gameEcpmRecord);
            //更新到用户余额
            BigDecimal userMoney = gameUser.getUserMoney();
            userMoney = userMoney.add(userTotal);
            gameUser.setUserMoney(userMoney);
            //插入数据库  判断是否插入成功
            gameUserService.updateGameUser(gameUser);
            //更新红包记录
            GamePacketRecord gamePacketRecord = new GamePacketRecord();
            gamePacketRecord.setUserName(gameUser.getUserName());
            gamePacketRecord.setUserId(gameUser.getUserId());
            gamePacketRecord.setPacketMoney(userTotal);
            gamePacketRecord.setPacketChannel(ecpmDesc);
            gamePacketRecord.setCreationTime(DateUtil.date());
            gamePacketRecordService.insertGamePacketRecord(gamePacketRecord);

        } catch (Exception e) {
            System.out.println(e);
            return AjaxResult.error(102,"系统异常");
        }
        HashMap<String, Object> objectStringHashMap = new HashMap<>();
        objectStringHashMap.put("userMoney",gameUser.getUserMoney());
        objectStringHashMap.put("adTotalReady",adTotalSurplus);
        return AjaxResult.success(objectStringHashMap);
    }




}
